<div ng-controller="orderDetailC">
    <div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form name="myform1">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">更改送货信息</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>车辆信息(每条信息以英文逗号分隔)</label>
                            <input type="text" ng-pattern="/^[a-zA-Z0-9\u4e00-\u9fa5,]+$/" class="form-control" ng-model="orderdetaildata.VehicleInfos">
                        </div>
                        <div class="form-group">
                            <label>联系人(每条信息以英文逗号分隔)</label>
                            <input type="text" ng-pattern="/^[a-zA-Z0-9\u4e00-\u9fa5,]+$/" class="form-control" ng-model="orderdetaildata.Contact">
                        </div>
                        <div class="form-group">
                            <label>联系人手机号(每条信息以英文逗号分隔)</label>
                            <input type="text" ng-pattern="/^[0-9,]+$/" class="form-control" ng-model="orderdetaildata.ContactPhone">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-success" ng-disabled="myform1.$invalid" ng-click="okchange()">确认提交</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">计划单详情</h1>----
        <span ng-if="orderdetaildata.OrderType == 0">普通订单</span>
        <span ng-if="orderdetaildata.OrderType == 1">商品锁价</span>
        <span ng-if="orderdetaildata.OrderType == 2">基价锁价</span>
        <span ng-if="orderdetaildata.OrderType == 3">自采订单</span>
        <span ng-if="orderdetaildata.OrderType == 4">库提销售</span>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="form-group">
                                <label>客户</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.Customer.ShortName}}">
                            </div>
                            <div class="form-group">
                                <label>公司户头</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.Account.Name}}">
                            </div>
                            <div class="form-group">
                                <label>区域</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.District.FillName}}">
                            </div>
                            <div class="form-group">
                                <label>客户经理</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.Creater.Name}}">
                            </div>
                            <div class="form-group">
                                <label>创建时间</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.CreateTime}}">
                            </div>
                            <div class="form-group">
                                <label>具体地址</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.Address}}">
                            </div>
                            <div class="form-group">
                                <label for="xiankuan" style="margin-right: 10px;">是否先款</label>
                                <label class="i-checks">
                                    <input type="checkbox" id="xiankuan" disabled="disabled" ng-model="orderdetaildata.IsFullPayment">
                                    <i></i>
                                </label>
                                <label for="suojia" style="margin-right: 10px;">是否锁价</label>
                                <label class="i-checks">
                                    <input class="changeprice" id="suojia"  disabled="disabled" type="checkbox" ng-model="orderdetaildata.IsLockPrice">
                                    <i></i>
                                </label>
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label for="suojia" style="margin-right: 10px;">是否锁价</label>-->
                                <!--<label class="i-checks">-->
                                    <!--<input class="changeprice" id="suojia"  disabled="disabled" type="checkbox" ng-model="orderdetaildata.IsLockPrice">-->
                                    <!--<i></i>-->
                                <!--</label>-->
                            <!--</div>-->
                            <div class="form-group">
                                <label for="biaojian" style="margin-right: 10px;">是否标件</label>
                                <label class="i-checks">
                                    <input type="checkbox" id="biaojian" disabled="disabled" ng-model="orderdetaildata.IsStandard">
                                    <i></i>
                                </label>
                                <label for="songhuo" style="margin-right: 10px;">是否自派</label>
                                <label class="i-checks">
                                    <input type="checkbox" id="songhuo" disabled="disabled" ng-model="orderdetaildata.IsDelivery">
                                    <i></i>
                                </label>
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label for="songhuo" style="margin-right: 10px;">是否送货</label>-->
                                <!--<label class="i-checks">-->
                                    <!--<input type="checkbox" id="songhuo" disabled="disabled" ng-model="orderdetaildata.IsDelivery">-->
                                    <!--<i></i>-->
                                <!--</label>-->
                            <!--</div>-->
                            <div class="form-group" ng-if="orderdetaildata.IsDelivery">
                                <label style="margin-right: 10px;">运费是否公户</label>
                                <label class="i-checks">
                                    <input type="checkbox" disabled="disabled" ng-model="orderdetaildata.IsPublicDelivery">
                                    <i></i>
                                </label>
                            </div>
                            <div class="form-group"">
                                <label>每吨运费</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.DeliveryPrice}}">
                            </div>
                            <div class="form-group" ng-if="!orderdetaildata.IsDelivery">
                                <label>车辆信息(每条信息以英文逗号分隔)</label>
                                <input type="text" ng-pattern="/^[a-zA-Z0-9\u4e00-\u9fa5,]+$/" class="form-control" readonly="readonly" ng-model="orderdetaildata.VehicleInfos">
                            </div>
                            <div class="form-group" ng-if="!orderdetaildata.IsDelivery">
                                <label>联系人(每条信息以英文逗号分隔)</label>
                                <input type="text" ng-pattern="/^[a-zA-Z0-9\u4e00-\u9fa5,]+$/" class="form-control" readonly="readonly" ng-model="orderdetaildata.Contact">
                            </div>
                            <div class="form-group" ng-if="!orderdetaildata.IsDelivery">
                                <label>联系人手机号(每条信息以英文逗号分隔)</label>
                                <input type="text" ng-pattern="/^[0-9,]+$/" class="form-control" readonly="readonly" ng-model="orderdetaildata.ContactPhone">
                            </div>
                            <div class="form-group">
                                <label>备注</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.Remarks}}">
                            </div>
                            <div class="form-group">
                                <label>申请单价优惠</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.ApplyDiscount}}">
                            </div>
                            <div class="form-group">
                                <label>申请总价优惠</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.BargainingAmount}}">
                            </div>
                            <div class="form-group">
                                <label ng-if="orderdetaildata.Provider.ProviderType == 0">钢厂</label>
                                <label ng-if="orderdetaildata.Provider.ProviderType == 1">仓库</label>
                                <label ng-if="orderdetaildata.Provider.ProviderType == 2">贸易商</label>
                                <input type="text" class="form-control" readonly="readonly" value="{{orderdetaildata.Provider.Name}}">
                            </div>
                            <div class="form-group">
                                <label>已付金额：</label>
                                <span>{{orderdetaildata.Prepayments | number:2}}</span>
                            </div>
                            <div ng-if="orderdetaildata.Prepayments+orderdetaildata.PaidDelivery >= orderdetaildata.DeliveryPriceTotal+orderdetaildata.Total" class="form-group">
                                <label>计划单已付清</label>
                            </div>
                            <div ng-if="orderdetaildata.Prepayments+orderdetaildata.PaidDelivery < orderdetaildata.DeliveryPriceTotal+orderdetaildata.Total" class="form-group">
                                <label style="color: red">计划单未付清</label>
                            </div>
                            <div class="form-group">
                                <label>创建人：</label>
                                <span>{{orderdetaildata.Creater.Name}}</span>
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label>变更记录：</label>-->
                                <!--<ul>-->
                                    <!--<li ng-repeat="item in orderdetaildata.Records | orderBy:['CreateTime']">-->
                                        <!--{{item.CreateTime}}/<label ng-if="item.Price != 0">{{item.Price | number:2}}/</label>{{item.Remarks}}-->
                                    <!--</li>-->
                                <!--</ul>-->
                            <!--</div>-->
                            <div class="form-group">
                                <accordion close-others="oneAtATime">
                                    <accordion-group heading="查看历史记录➤">
                                        <ul>
                                            <li ng-repeat="item in orderdetaildata.Records | orderBy:['CreateTime']">
                                                {{item.CreateTime}}/<label ng-if="item.Price != 0">{{item.Price | number:2}}/</label>{{item.Remarks}}
                                            </li>
                                        </ul>
                                    </accordion-group>
                                </accordion>
                            </div>
                            <div class="form-group">
                                <label>订单状态：</label>
                                    <span style="width:10%" ng-if="orderdetaildata.State == 0">待确认</span>
                                    <span style="width:10%" ng-if="orderdetaildata.State == 1">驻厂确认</span>
                                    <span style="width:10%" ng-if="orderdetaildata.State == 2">已发货</span>
                                    <span style="width:10%" ng-if="orderdetaildata.State == 3">已收货</span>
                                    <span style="width:10%" ng-if="orderdetaildata.State == 4">已结算</span>
                                    <span style="width:10%" ng-if="orderdetaildata.State == 5">完结</span>
                                    <span style="width:10%" ng-if="orderdetaildata.State == 6">取消</span>
                                    <span style="width:10%" ng-if="orderdetaildata.State == 7">审核通过</span>
                                    <span style="width:10%" ng-if="orderdetaildata.State == 8">已退货</span>
                                <span style="width:10%" ng-if="orderdetaildata.State == 9">已退款</span>
                            </div>
                        </div>
                        <div class="container-fluid text-center bg-light lter" >
                            <div class="row">
                                <h3 style="display: inline-block;margin: 0 auto">订单商品列表</h3>
                            </div>
                        </div>
                        <div class="container-fluid" style="margin-top: 10px">
                            <!--<div class="row" style="border-bottom: red solid 1px" ng-repeat="item in orderdetaildata.CommodityInfos">-->
                                <!--<div class="col-xs-12 col-md-12">-->
                                    <!--<div>钢厂：{{item.Commodity.Brand}}</div>-->
                                    <!--<div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>-->
                                    <!--<div>单价：{{item.Price | number:2}}/t</div>-->
                                    <!--<div>数量：{{item.Count}}</div>-->
                                    <!--<div>总质量：{{item.Weight | number:3}}t</div>-->
                                    <!--<div>总价：{{item.Price | number:2}}*{{item.Weight | number:3}}t={{item.Price*item.Weight | number:2}}</div>-->
                                <!--</div>-->
                            <!--</div>-->


                            <div class="panel panel-default" ng-repeat="item in orderdetaildata.CommodityInfos" style="margin-bottom: 10px">
                                <div class="panel-heading">
                                    <div>品牌：{{item.Commodity.Brand}}</div>
                                </div>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-xs-12 col-md-12">
                                            <div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>
                                            <div>单价：{{item.Price | number:2}}/t</div>
                                            <div>数量：{{item.Count}}</div>
                                            <div>总质量：{{item.Weight | number:3}}t</div>
                                        </div>
                                    </div>
                                </div>
                                <div class="panel-footer">
                                    <div>总价：{{item.Price | number:2}}*{{item.Weight | number:3}}t={{item.Price*item.Weight | number:2}}</div>
                                </div>
                            </div>
                            <div class="row text-left container-fluid" ng-if="orderdetaildata.UnitTotal > 0">
                                <span>先款和非标优惠合计：{{orderdetaildata.UnitTotal| number:2}}</span>
                            </div>
                            <!--<div class="row text-left container-fluid" ng-if="orderdetaildata.NonstandardDiscount > 0">-->
                                <!--<span>非标优惠：{{orderdetaildata.NonstandardDiscount | number:2}}</span>-->
                            <!--</div>-->
                            <div class="row text-left container-fluid" ng-if="orderdetaildata.ApplyDiscount > 0">
                                <span>单价优惠：{{orderdetaildata.ApplyDiscount | number:2}}</span>
                            </div>
                            <div class="row text-left container-fluid">
                                <h4 style="display: inline-block">总价：{{orderdetaildata.Total| number:2}}</h4>
                            </div>
                            <div class="row text-left container-fluid">
                                <h4 style="display: inline-block">运费：{{orderdetaildata.DeliveryPriceTotal | number:2}}</h4>
                            </div>
                            <!--<div class="row text-left container-fluid">-->
                                <!--<h4 style="display: inline-block">总价：{{orderdetaildata.Total | number:2}}</h4>-->
                            <!--</div>-->
                            <!--<div class="row text-left container-fluid">-->
                                <!--<h4 style="display: inline-block">预计运费价格：{{orderdetaildata.DeliveryPriceTotal | number:2}}</h4>-->
                            <!--</div>-->
                            <div class="row text-right">
                                <button type="button" ng-if="orderdetaildata.State != 5 && orderdetaildata.State != 6 && orderdetaildata.State != 8" class="btn btn-success" ng-click="changeuserdata()">更改客户基本信息</button>
                                <button type="button" ng-if="(orderdetaildata.State==2 ||orderdetaildata.State==0||orderdetaildata.State==1||orderdetaildata.State==7)&& orderdetaildata.IsSettlement!=1 && orderdetaildata.State != 5 || orderdetaildata.State != 6 &&  orderdetaildata.State != 8 " class="btn btn-success" ng-click="orderdetaildata.IsSettlement ? buneng() : changeorderdata()">更改商品信息</button>
                                <button type="button" ng-if="orderdetaildata.State == 7 && orderdetaildata.Provider.ProviderType == 1 && orderdetaildata.Provider.OperationsMan.Id==user.Id" class="btn btn-success" ng-click="ordersub()">确认发货</button>
                                <button ng-if="orderdetaildata.IsDelivery==1" type="button" class="btn btn-success" data-toggle="modal" data-target="#dlg">更改送货信息</button>
                                <button ng-if="(orderdetaildata.State != 6 && orderdetaildata.State != 8 && orderdetaildata.State != 9 && orderdetaildata.ProcessState != 1 && orderdetaildata.ProcessState != 2) || ((orderdetaildata.ProcessState == 1 || orderdetaildata.ProcessState == 2) && orderdetaildata.State == 7)" type="button" class="btn btn-success" ng-click="printPact()">物联合同</button>
                                <button ng-if="(orderdetaildata.State != 6 && orderdetaildata.State != 8 && orderdetaildata.State != 9 && orderdetaildata.ProcessState != 1 && orderdetaildata.ProcessState != 2) || ((orderdetaildata.ProcessState == 1 || orderdetaildata.ProcessState == 2) && orderdetaildata.State == 7)" type="button" class="btn btn-success" ng-click="printPactMW()">沐威合同</button>
                                <button ng-if="orderdetaildata.State == 0 || orderdetaildata.State == 7" type="button" class="btn btn-danger" ng-click="payment()">付款</button>
                                <button ng-if="orderdetaildata.State == 2 && !orderdetaildata.IsSettlement" type="button" class="btn btn-danger" ng-click="clearing()">结算</button>
                                <button type="button" class="btn btn-default" ng-click="goorderList()">返回计划单列表</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
    //计划单详情控制器
    .controller("orderDetailC", function ($scope, $state, $stateParams, $http) {
        $scope.id = JSON.parse($stateParams.orderid);
        //获取计划单详细数据
        $scope.AllPrice=[]
        $scope.sprice=0;
        $http({
            url: "../api/Order/Detail?id=" + $scope.id,
            method: "GET"
        })
            .then(function (res) {
                $scope.orderdetaildata = res.data;
                console.log($scope.orderdetaildata);
                for (var i = 0; i < $scope.orderdetaildata.CommodityInfos.length; i++) {
                    $scope.AllPrice.push(($scope.orderdetaildata.CommodityInfos[i].Price-$scope.orderdetaildata.ApplyDiscount)*$scope.orderdetaildata.CommodityInfos[i].Weight);
                    // 往all里添加（单价-单价优惠）* 重量=实际价格
                     $scope.sprice+=$scope.AllPrice[i]
                     // 把每个实际价格相加得到总价
                }
                $scope.sprice-=$scope.orderdetaildata.UnitTotal;
                // 总价-先款非标优惠合计=实际总价；
                console.log($scope.sprice)
            })
            .then(function (error) {
                if (error) {
                    console.log("error");
                }
            });
            //提交更改
            $scope.okchange = function(){
                    $http.post('../api/OrderManager/OrderBaseEdit', $scope.orderdetaildata)
                            .success(function (res) {
                                if (!res) {
                                    alert("修改失败");
                                } else {
                                    alert("修改成功");
                                    $('#dlg').modal("hide");
                                    $state.go('app.Order_Detail', {
                                        orderid: JSON.stringify($scope.id)
                                    }); window.location.reload()
                                }
                            });
                };
        //更改客户基本信息
        $scope.buneng=function(){
            alert('商品已结算，信息不能修改！！！')
        }
        $scope.changeuserdata = function () {
            $state.go('app.Order_OrderBaseEdit', {
                orderid: JSON.stringify($scope.id)
            });
        };
        //更改商品信息
        $scope.changeorderdata = function () {
            $state.go('app.Order_CommodityInfoEdit', {
                orderid: JSON.stringify($scope.id)
            });
        };
        //返回计划单列表
        $scope.goorderList = function () {
            $state.go('app.Order_orderList');
        };
        //结算
        $scope.clearing = function () {
            $http({
                url: "../api/OrderManager/OrderSettlement?orderId=" + $scope.id,
                method: "POST"
            })
                .then(function (res) {
                    if (res.State <= 0) {
                        alert(res.Msg);
                    } else {
                        alert("结算成功");
                        $scope.goorderList();
                        window.location.reload()
                    }
                })
        };

        $scope.printPact = function () {
            window.open("../Print/SalesContract?orderId=" + $scope.id);
        };
        $scope.printPactMW = function () {
            window.open("../Print/SalesContractMW?orderId=" + $scope.id);
        };
        //付款
        $scope.payment = function () {
            $http({
                url: "../api/Order/Payment?id=" + $scope.id,
                method: "POST"
            })
                .then(function () {
                    alert("付款成功");
                    $state.go('app.Order_Detail', {
                        orderid: JSON.stringify($scope.id)
                    });
                    window.location.reload();
                })
                .then(function (error) {
                    if (error) {
                        console.log("error");
                    }
                });
        };
        $scope.ordersub = function () {
            console.log($scope.orderdetaildata)
            $http.put("../api/OrderManager/EditSendInfo", $scope.orderdetaildata)
                    .success(function (res) {
                        if (res.State <= 0) {
                            alert(res.Msg);
                        } else {
                            alert("出库成功");
                            $state.go('app.Order_Detail', {
                                orderid: JSON.stringify($scope.id)
                            });
                            window.location.reload();
                        }
                    });
        };
    })
</script>